/**
 * Created by Lumix on 2015/04/15.
 */

var db = require('../../utils/db/mysqlDb');
var accountModel = require('../model/Account');
var common = require('../../utils/commonUtils');
var config = require('../../../config');
var Promise = require('promise');


var accountDao = {};

var defaultSelectColumn = ' account_id,account_appid,account_appsecret,account_name,account_remark,account_domain,account_createtime';

var defaultTable = 'wechat_account';
//getById
accountDao.getById = function(id){
	return new Promise(function(fulfill, reject){
		var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable+ ' where 1=1 ';
		if(typeof id === "object"){
			sql+='and account_id= ' + db.escape(id.id);
		}else{
			sql+=' and account_id= ' + db.escape(id);
		}
		db.query(sql).then(function(rows){
			if(rows[0]){
				fulfill(accountModel.dbConvertModel(rows[0]));
			}else{
				fulfill(null);
			}
		}, function(err){
			reject(err);
		});
	});
};

//save
accountDao.save = function(obj, connection){
	return db.save('insert into ' + defaultTable+ '  set ?',
		accountModel.modelConvertDb(obj), connection);
};

//update
accountDao.update = function(obj, connection){
	var sql = 'update ' + defaultTable+ '  set ';
	if(typeof obj.appId != "undefined"){
		sql+='account_appid=:appId,';
	}
	if(typeof obj.appSecret != "undefined"){
		sql+='account_appsecret=:appSecret,';
	}
	if(typeof obj.name != "undefined"){
		sql+='account_name=:name,';
	}
	if(typeof obj.remark != "undefined"){
		sql+='account_remark=:remark,';
	}
	if(typeof obj.domain != "undefined"){
		sql+='account_domain=:domain,';
	}
	if(common.isNotBlank(obj.createTime)){
		sql+='account_createtime=:createTime,';
	}
	sql+='where ';
	sql+='account_id=:id ';
	sql = sql.replace(',where', '  where');
	return db.update(sql, obj, connection);
};

//delete
accountDao.delete = function(id, connection){
	var sql = 'delete from ' + defaultTable+ '  where 1=1 ';
	if(typeof id === "object"){
		sql+=' and account_id= ' + db.escape(id.id);
	}else{
		sql+=' and account_id= ' + db.escape(id);
	}
	return db.update(sql, {}, connection);
};

//查询条件
var selectParams = function(params){
	var sql = '';
	if(params && typeof params.id != "undefined"){
		sql+=' and  account_id=' + db.escape(params.id);
	}
	if(params && typeof params.appId != "undefined"){
		sql+=' and  account_appid=' + db.escape(params.appId);
	}
	if(params && typeof params.appSecret != "undefined"){
		sql+=' and  account_appsecret=' + db.escape(params.appSecret);
	}
	if(params && typeof params.name != "undefined"){
		sql+=' and  account_name=' + db.escape(params.name);
	}
	if(params && typeof params.remark != "undefined"){
		sql+=' and  account_remark=' + db.escape(params.remark);
	}
	if(params && typeof params.domain != "undefined"){
		sql+=' and  account_domain=' + db.escape(params.domain);
	}
	if(params && typeof params.createTime != "undefined"){
		sql+=' and  account_createtime=' + db.escape(params.createTime);
	}
	return sql;
};


//count
accountDao.count = function(params){
	return new Promise(function(fulfill, reject){
		var sql = 'select count(*) as total from ' + defaultTable;
		sql += ' where 1=1 ';
		sql += selectParams(params);
		db.query(sql).then(function(rows){
			fulfill(rows[0].total);
		}, function(err){
			reject(err);
		});
	});
};

//page
accountDao.getByPage = function(params, pageNo, pageSize, orderby){
	return new Promise(function(fulfill, reject){
		var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
		sql+=' where 1=1 ';
		sql += selectParams(params);
		sql+=' order by ' + orderby;
		if(pageNo <= 0){
			pageNo = 1;
		}
		pageNo = common.isNumber(pageNo) ? pageNo : 1;
		pageSize = common.isNumber(pageSize) ? pageSize : 10;
		sql += ' limit ';
		sql += (pageNo - 1) * pageSize;
		sql +=',';
		sql +=pageSize;

		db.query(sql).then(function(rows){
			var array = new Array();
			for(var p in rows){
				if(rows[p])array.push(accountModel.dbConvertModel(rows[p]));
			}
			fulfill(array);
		}, function(err){
			reject(err);
		});
	});
};

accountDao.getByParams = function(params, orderby){
	return new Promise(function(fulfill, reject){
		var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
		sql+=' where 1=1 ';
		sql += selectParams(params);
		sql+=' order by ' + orderby;
		db.query(sql).then(function(rows){
			var array = new Array();
			for(var p in rows){
				if(rows[p])array.push(accountModel.dbConvertModel(rows[p]));
			}
			fulfill(array);
		}, function(err){
			reject(err);
		});
	});
};

//获取默认账户
accountDao.getDefaultAccount = function() {
	return new Promise(function(fulfill, reject){
		var sql = 'select ' + defaultSelectColumn + ' from ' + defaultTable;
		sql +=' where account_id = '+ config.wechat.defaultAccountId;
		db.query(sql).then(function(rows){
			fulfill(accountModel.dbConvertModel(rows[0]));
		}, function(err){
			reject(err);
		});
	});
};


accountDao.db = db;
module.exports = accountDao;